//
//  KLHomeViewController.m
//  BIBILILI
//
//  Created by WTFKL on 2017/9/18.
//  Copyright © 2017年 WTF. All rights reserved.
//

#import "KLHomeViewController.h"
#import "KLCustomNavBar.h"
#import "KLLiveViewViewController.h"
#import "KLRecommendViewController.h"
#import "KLFollowViewController.h"


@interface KLHomeViewController ()<customNavBarDelegate,UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView * scrollView;
@property(nonatomic,strong) KLCustomNavBar *customNavigationBar;
@property(nonatomic,strong) NSMutableArray * subVCArray;

@end

@implementation KLHomeViewController

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44+__KLSTATUSBARHEIGHT__, self.view.width, self.view.height)];
        _scrollView.contentSize = CGSizeMake(self.view.width*self.subVCArray.count, self.view.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(NSMutableArray *)subVCArray{
    if (!_subVCArray) {
        _subVCArray = [NSMutableArray arrayWithObjects:@"KLLiveViewViewController",@"KLRecommendViewController",@"KLFollowViewController", nil];
    }
    return _subVCArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

}

-(KLCustomNavBar *)customNavigationBar{
    if (!_customNavigationBar) {
        _customNavigationBar = [[KLCustomNavBar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44+__KLSTATUSBARHEIGHT__)];
        _customNavigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar"]];
        _customNavigationBar.delegate = self;
    }
    return _customNavigationBar;
}

-(void)setNavigationBar{
    self.navigationController.navigationBar.hidden =YES;
    [self.navigationController.view addSubview:self.customNavigationBar];

}




-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView.delegate = self;
    [self setNavigationBar];
    [self setScrollViewInHere];
    [self.scrollView scrollRectToVisible:CGRectMake(self.view.width, 0, self.view.width, self.view.height) animated:YES];
}

-(void)setScrollViewInHere{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    KLLiveViewViewController  * liveVC = [[KLLiveViewViewController alloc] initWithCollectionViewLayout:layout];
    KLRecommendViewController * recommendVC = [[KLRecommendViewController alloc] initWithCollectionViewLayout:layout];
    KLFollowViewController    * followVC = [[KLFollowViewController alloc] initWithCollectionViewLayout:layout];
    [self addChildViewController:liveVC];
    [self addChildViewController:recommendVC];
    [self addChildViewController:followVC];
    [self.view addSubview:self.scrollView];
}
//根据tag设置导航
-(void)pushtoViewWithTag:(NSInteger)tag{
    CGPoint contentSet = self.scrollView.contentOffset;
    contentSet.x=tag*self.scrollView.width;
    [self.scrollView setContentOffset:contentSet animated:YES];
}

//结束滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [UIView animateWithDuration:0.2 animations:^{
        if (scrollView.contentOffset.x/self.view.width == 0) {
                self.customNavigationBar.line.kl_centerX = self.customNavigationBar.liveButton.kl_centerX;
        }else if (scrollView.contentOffset.x/self.view.width == 1){
                self.customNavigationBar.line.kl_centerX = self.customNavigationBar.recomendButton.kl_centerX;
        }else if (scrollView.contentOffset.x/self.view.width == 2){
            self.customNavigationBar.line.kl_centerX = self.customNavigationBar.followButton.kl_centerX;
        }
    }];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self addChildViewToScrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self addChildViewToScrollView];
}
//滚动到对应子视图再创建控制器
-(void)addChildViewToScrollView{
    NSUInteger index =self.scrollView.contentOffset.x/self.scrollView.width;
    UIViewController *childVc =self.childViewControllers[index];
    UICollectionView *collectionView = (UICollectionView *)childVc.view;
    collectionView.frame =self.scrollView.bounds;
    
    [self.scrollView addSubview:childVc.view];
}


@end
